package com.xjgc.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    public static boolean maxDay() {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        boolean flag=false;
        if (calendar.get(Calendar.DATE)==calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            flag=true;
        };
        return flag;
    }
    public   static Date getLastDateOfPrevMonth(){
        Calendar c = Calendar.getInstance();
        //设置为指定日期
        c.setTime(new Date());
        //指定日期月份减去一
        c.add(Calendar.MONTH, -1);
        //指定日期月份减去一后的 最大天数
        c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
        //获取最终的时间
        Date lastDateOfPrevMonth = c.getTime();
        return lastDateOfPrevMonth;
    }
    public static String today(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String today=format.format(new Date());
        return today;
    }
    public static String yestoday() throws ParseException {
        String today=DateUtil.today();
        String[] todayArray=today.split("-");
        if("01".equals(todayArray[2])){
            Date date=DateUtil.getLastDateOfPrevMonth();
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            return format.format(date);
        }else {
            today = today.replaceAll("-", "");
            Integer todayNo = Integer.parseInt(today);
            Integer yestodyNo=todayNo.intValue() - 1;
            SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
            format.parse(yestodyNo.toString());
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            return dateFormat.format(yestodyNo);
        }
    }

    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return cal.getTime();
    }
    // 获取本周结束时间
    public static Date getEndDayOfWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return weekEndSta;
    }
    // 获取本月的开始时间
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return calendar.getTime();
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return calendar.getTime();
    }
    // 获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    // 获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
//        Date date=format.parse("20190308");
//        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(dateFormat.format(date));
        System.out.println(DateUtil.getNowMonth());
    }

}
